<?php

namespace Admin\Controller;

use Think\Controller;
use Common\Controller\AuthController;
use Think\Auth;

class BannerController extends AuthController {

    //列表
    public function banner_list() {
        $count = M('banner')->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show(); // 分页显示输出
        $banner = M('banner')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('banners', $banner);
        $this->assign('page', $show);
        $this->display();
    }

    //添加banner
    public function banner_add() {
        if(IS_AJAX){
            var_dump(I());exit;
        }
        $data=M('banner_type')->where(array('open'=>1))->select();
        $this->assign('banner_type',$data);
        $this->display();
    }

    //栏目管理
    public function news_column() {
        $column = M('column');
        $nav = new \Org\Util\Leftnav;   //树状结构
        $column = $column->order('column_order')->select();
        $arr = $nav::column($column);
        $this->assign('arr', $arr);
        $this->display();
    }

    //添加栏目
    public function news_columnadd() {
        $column_leftid = I('c_id', 0);
        $this->assign('column_leftid', $column_leftid);
        $this->display();
    }

    //修改栏目
    public function news_columnedit() {
        $id = I('c_id');
        $column=M('column');
        $column = $column->where(array('c_id'=>$id))->find();
        $this->assign('column', $column);
        $this->display();
    }
    //栏目增加入库
    public function runnews_columnadd() {
        if (!IS_AJAX) {
            $this->error('提交方式不正确', U('News/news_column'), 0);
        } else {
            $data = array(
                'column_name' => I('column_name'),
                'column_enname' => I('column_enname'),
                'column_type' => I('column_type'),
                'column_leftid' => I('column_leftid'),
                'column_address' => I('column_address'),
                'column_open' => I('column_open', 0),
                'column_order' => I('column_order'),
                'column_title' => I('column_title'),
                'column_key' => I('column_key'),
                'column_des' => I('column_des'),
                'column_content' => I('kind_text'),
                'column_encontent' => I('kind_entext'),
            );
            M('column')->add($data);
            $this->success('栏目保存成功', U('News/news_column'), 1);
        }
    }
    //栏目修改入库
     public function runnews_columnedit() {
        if (!IS_AJAX) {
            $this->error('提交方式不正确', U('News/news_column'), 0);
        } else {
            $data = array(
                'column_name' => I('column_name'),
                'column_enname' => I('column_enname'),
                'column_type' => I('column_type'),
                'column_address' => I('column_address'),
                'column_open' => I('column_open', 0),
                'column_order' => I('column_order'),
                'column_title' => I('column_title'),
                'column_key' => I('column_key'),
                'column_des' => I('column_des'),
                'column_content' => I('kind_text'),
                'column_encontent' => I('kind_entext'),
            );
           $result = M('column')->where('c_id='.I('column_id'))->save($data);
           if($result){
            $this->success('栏目保存成功', U('News/news_column'), 1);
           }else{
               $this->error('保存失败',U('News/news_column'), 0);
           }
        }
    }

    //删除栏目
    public function news_columndel() {
        $column = M('column')->where(array('c_id' => I('c_id')))->delete();
        $this->redirect('news_column');
    }
    
    //多选 删除栏目
    public function leftnavalldel() {
        if(I('left_id')){
            $ids=implode(',',I('left_id'));
        }
        M('column')->delete($ids);
        $this->success('栏目删除成功', U('News/news_column'), 1);
    }
    
    //多选 排序栏目
    public function leftnavorder() {
        if(I('left_id')){
            $column=M('column');
            foreach(I('left_id') as $key=>$val){
                $result = $column->where('c_id='.$val)->setField('column_order',I($val));
            }
        }
        //var_dump(U('News/news_column'));exit;
        $this->success('排序成功', U('News/news_column'), 1);
    }

}
